package cn.b.training.baby_education_training.repository;

import cn.b.training.baby_education_training.entity.BabyInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @program: baby_education
 * @description
 * @author: wuying
 * @create: 2022-11-29 10:26
 **/
@Repository
public interface BabyInfoRepository extends JpaRepository<BabyInfo,Long> {

    /**
     * 通过id获取婴儿信息
     * @param id
     * @return
     */
    @Query("select a from BabyInfo a where a.id=?1")
    BabyInfo getByBabyId(Long id);

    @Query("select a from BabyInfo a where a.userId=?1")
    List<BabyInfo> getBabyByUserId(Long userId);

    @Query("select a.babyName from BabyInfo a where a.id=?1")
    String findBaby(Long id);

    /**
     * 插入婴儿信息
     * @param babyInfo
     * @return
     */
    @Query(value = "insert into BabyInfo(userId) value(?1)", nativeQuery = true)
    int insertBabyInfo(BabyInfo babyInfo);

    /**
     * 修改头像
     * @param id 用户Id
     * @param babyAvatar 头像
     * @return
     */
    @Modifying
    @Transactional(rollbackFor = RuntimeException.class)
    @Query(value = "update baby_info set baby_avatar=?2 where id = ?1", nativeQuery = true)
    int updateAvatar(Integer id, String babyAvatar);
}
